package com.cqu.apptest.controller;

import com.cqu.apptest.pojo.User;
import com.cqu.apptest.service.UserService;
import com.cqu.springmvc.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller //默认是类名，但是首字母小写 userController
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/user/query")
    public String queryUser(HttpServletRequest request, HttpServletResponse response, String name) {
        //处理response的中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        String userMessage = userService.queryUserByName(name);
        request.setAttribute("userMessage", userMessage);
        //转发到user.jsp
        return "forward:/user.jsp";
    }

    @RequestMapping("/user/findAll")
    @ResponseBody
    public List<User> findAllUsers(HttpServletRequest request, HttpServletResponse response){
        return userService.findUsers();
    }

}
